﻿using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Runtime.Serialization;
using System.Windows.Controls;
using System.Windows.Input;
using Techstar.Dialog;
using SmartMD.DataModel;


namespace SmartMD.Manager.StartPage
{
    /// <summary>
    /// Interaction logic for RecentPanel.xaml
    /// </summary>
    public partial class RecentPanel : UserControl
    {
        private static RecentPanel _instance;
        private ObservableCollection<RecentItem> _items = new ObservableCollection<RecentItem>();

        public RecentPanel()
        {
            InitializeComponent();
            _instance = this;

            if (SingletonManager.Cache.ExistsEntity("StartPage", "RecentItem"))
                _items = SingletonManager.Cache.LoadEntity<ObservableCollection<RecentItem>>("StartPage", "RecentItem");
            BindingList();
        }

        public static void Save()
        {
            if (_instance == null)
                return;
            var list = _instance._items.OrderByDescending(item => item.DateTime).ThenByDescending(item => item.Order).ToList();
            while (list.Count() > 15)
            {
                list.RemoveAt(15);
            }
            var items = new ObservableCollection<RecentItem>();
            foreach (var recentItem in list)
            {
                items.Add(recentItem);
            }
            SingletonManager.Cache.SaveEntity("StartPage", "RecentItem", items);
        }

        public static void RemoveItem(string id)
        {
            var recentItem = _instance._items.FirstOrDefault(item => item.Id == id);
            if (recentItem != null)
            {
                _instance._items.Remove(recentItem);
                _instance.BindingList();
                Save();
            }
        }

        public static void AddItem(string id, string name)
        {
            var recentItem = _instance._items.FirstOrDefault(item => item.Id == id);
            if (recentItem == null)
            {
                recentItem = new RecentItem(id, name);
                _instance._items.Add(recentItem);
            }
            else
                recentItem.Order++;
            recentItem.DateTime = DateTime.Now;
            _instance.BindingList();
        }

        private void BindingList()
        {
            
        }

        private void OnSelectItem(object sender, MouseButtonEventArgs e)
        {
           
        }
    }

    [DataContract]
    public class RecentItem : EntityBase
    {
        private string _id;
        private string _name;

        public RecentItem()
        {

        }

        public RecentItem(string id, string name)
        {
            _id = id;
            _name = name;
        }

        public string Id
        {
            get
            {
                return _id;
            }
            set
            {
                _id = value;
                this.OnPropertyChanged("Id");
            }
        }

        public string Name
        {
            get
            {
                return _name;
            }
            set
            {
                _name = value;
                this.OnPropertyChanged("Name");
            }
        }

        public DateTime DateTime { get; set; }

        public int Order { get; set; }
    }
}
